package cn.pjz.dataUpload.web.controller;

import cn.pjz.basic.result.JSONResult;
import cn.pjz.dataUpload.entity.RTransaction;
import cn.pjz.dataUpload.entity.RTransaction_ZFB;
import cn.pjz.dataUpload.service.RTransactionService;
import cn.pjz.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

import static cn.pjz.utils.constant.CONSTANT_WX;
import static cn.pjz.utils.constant.CONSTANT_ZFB;

@RestController
public class dataUploadController {


    @Autowired
    private RTransactionService rTransactionService;

    @PostMapping("/handleFileUpload_WX")
    public JSONResult handleFileUpload_WX(@RequestParam("file") MultipartFile file) {
        File file1 = new FileUtil().multipartFileToFile(file);

        List<RTransaction> users = FileUtil.read(",", file1, RTransaction.class);
        users.forEach(item->{
            if (item.getTransactingForm().equals("支出")) {
                UUID uuid = UUID.randomUUID();
                String uuidStr = uuid.toString().replaceAll("-", "");
                item.setTransactionId(uuidStr);

                String str = item.getTransactionDate();
                String[] parts = str.split(" ", 2);
                String[] parts1 = parts[0].split("-");

                String str1 = parts1[0] +
                        ((parts1[1].length() == 1) ? "0" + parts1[1] : parts1[1]) +
                        ((parts1[2].length() == 1) ? "0" + parts1[2] : parts1[2]);

                item.setTransactionDate(str1);
                item.setTransactionUser(1);
                item.setIntroducer(1);
                String transactionMoney = item.getTransactionMoney();
                String result = transactionMoney.replace("￥", "");
                result = transactionMoney.replace("¥", "");
                item.setTransactionMoney(result);
                item.setTransactionApp(CONSTANT_WX);
                rTransactionService.insertBy(item);
            }
        });
        return JSONResult.success("账单记录导入完成!");
    }

    @PostMapping("/handleFileUpload_ZFB")
    public JSONResult handleFileUpload_ZFB(@RequestParam("file") MultipartFile file) {
        File file1 = new FileUtil().multipartFileToFile(file);
        List<RTransaction_ZFB> users = FileUtil.read(",", file1, RTransaction_ZFB.class);
        users.forEach(item->{
            RTransaction rTransaction = new RTransaction();
            UUID uuid = UUID.randomUUID();
            String uuidStr = uuid.toString().replaceAll("-", "");
            rTransaction.setTransactionId(uuidStr);

            String dateStr = item.getTransactionDate();
            DateTimeFormatter formatterInput = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            DateTimeFormatter formatterOutput = DateTimeFormatter.ofPattern("yyyyMMdd");

            LocalDate date = LocalDate.parse(dateStr, formatterInput);
            String formattedDate = date.format(formatterOutput);

            rTransaction.setTransactionDate(formattedDate);
            rTransaction.setTransactionType(item.getTransactionType());
            rTransaction.setTransactionCommodity(item.getTransactionCommodity());
            rTransaction.setTransactionMoney(item.getTransactionMoney());
            rTransaction.setTransactionWay(item.getTransactionWay());
            rTransaction.setTransactingParty(item.getTransactingParty());

            rTransaction.setTransactionUser(1);
            rTransaction.setIntroducer(1);
            String transactionMoney = item.getTransactionMoney();
            String result = transactionMoney.replace("￥", "");
            item.setTransactionMoney(result);
            rTransaction.setTransactionApp(CONSTANT_ZFB);
            rTransactionService.insertBy(rTransaction);
        });
        return JSONResult.success("账单记录导入完成!");
    }



}
